package com.gitee.huxing.course.conc.conc01.sync;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.conc.cono01.sync
 * @ClassName: TestSetGet
 * @Author: huxing
 * @DateTime: 2021-08-20 下午12:21
 */
public class TestSetGet {

    public static void main(String[] args) throws Exception{

        final SetGet s = new SetGet();
        Thread t = new Thread(() -> {
            try {
                s.get();
            } catch (Exception ex){
                ex.printStackTrace();
            }
        });
        t.start();
        long start = System.currentTimeMillis();
        s.set(10);
        System.out.println(" ...." + (System.currentTimeMillis() - start));
    }

    public static class SetGet{

        int a = 0;

        public synchronized void set(int v) throws Exception{
            System.out.println(Thread.currentThread().getName() + " setting " + v);
            Thread.sleep(1000L);
            a = v;
            System.out.println(Thread.currentThread().getName() + " set " + v);
        }

        public synchronized int get() throws Exception{
            System.out.println(Thread.currentThread().getName() + " getting ");
            Thread.sleep(1000L);
            System.out.println(Thread.currentThread().getName() + " get "   );
            return a;
        }
    }
}
